<template>
  <div class="process">
    <jc-button
      :btn-arr="['reload', 'newy', 'sub', 'pass', 'option', 'remove']"
      :disab-arr="['com1', 'com2', 'cancel', 'freeze', 'close', 'using', 'chart']"
      @refresh="handleQuerySonClass"
      @newly="newly"
      @audit="audit(1)"
      @noAudit="audit(2)"
      @submit="proSubmit(1)"
      @noSubmit="proSubmit(2)"
      @del="delTechno"
      @toExcel="handleExport"
    />
    <div class="header">
      <div class="header-name">
        <search :options="selectData" :msg="tacitly" @seek="collect" @hand="handleQuerySonClass()" />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass()">{{ $t('btn.search') }}</el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="handleQuerySonClass"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        ref="dataTable"
        sortable
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        table-selection
        :get-summaries="getSummaries"
        show-summary
        @selectionChange="handleSelectAll"
      >
        <template v-slot:billSlot="clo">
          <el-link type="primary" @click="detailPurchase(clo.scope.row)">{{ clo.scope.row.fnumber }}</el-link>
        </template>
        <template v-slot:btnState="col">
          <el-tag :type="col.scope.row.stateColor">{{ col.scope.row.fdocumentstatus }}</el-tag>
        </template>
        <template v-slot:btnSlot="clo">
          <el-button type="primary" size="mini" @click="detailPurchase(clo.scope.row)">{{ $t('market.detail') }}</el-button>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
  </div>
</template>

<script>
import {
  deleteWorkshopCraftInfo,
  notReviewWorkshopCraft, notSubmitWorkshopCraft,
  queryWorkshopCraftList, reviewWorkshopCraft, submitWorkshopCraft
} from '@/api/processLibrary'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import { toMxBmina } from '@/components/ToMxamineState'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'

export default {
  name: 'TechnologyList',
  components: {
    jcButton,
    search,
    filtration
  },
  mixins: [searData],
  data() {
    return {
      ftype: 64,
      fbillNo: 'fname',
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      activeName: 'first',
      // 表头
      tableData: [], // 工序库数据
      tableHeight: '72vh',
      tableHeader: [
        { label: this.$t('market.craftnumber'), type: 'bill', prop: 'fnumber', minWidth: '110px' },
        { label: this.$t('market.fcraftname'), prop: 'fname', minWidth: '100px' },
        { label: this.$t('market.deaprt'), prop: 'fdeptName', minWidth: '110px' },
        { label: this.$t('market.fmodel2'), prop: 'fmodel', minWidth: '110px' },
        { label: this.$t('market.totalAmount'), prop: 'totalSumPrice', minWidth: '110px' },
        { label: this.$t('market.documentstatus'), type: 'state', prop: 'fdocumentstatus', minWidth: '100px' },
        { label: this.$t('market.createDate'), prop: 'fcreateTime', minWidth: '125px' },
        { label: this.$t('market.handle'), type: 'btn' }
      ],
      tableHeader1: [],
      val: []
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 220px)' : '72vh'
  },
  methods: {
    // 导出表格-销售订单列表
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          queryWorkshopCraftList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list.map(item => {
                return (toMxBmina(item))
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '工序库列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取订单列表数据
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = { pageNum: this.currentPage, pageSize: this.size, search: this.searCollData.concat(this.details) }
      const { data: RES } = await queryWorkshopCraftList(DATA)
      this.tableData = RES.list.map(item => {
        return (toMxBmina(item))
      })
      this.total = RES.total
    },
    // 新增
    newly() {
      this.$router.push({ path: '/technologyAdd' })
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 多选
    handleSelectAll(val) {
      this.selectVal = []
      val.forEach(res => {
        this.selectVal.push(res.fid + '')
      })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: this.selectVal }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.handleGetUntreated()
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    proSubmit(ev) {
      const api = ev === 1 ? submitWorkshopCraft : notSubmitWorkshopCraft
      this.toExamineFun(api)
    },
    // 审核
    audit(ev) {
      const api = ev === 1 ? reviewWorkshopCraft : notReviewWorkshopCraft
      this.toExamineFun(api)
    },
    // 删除工艺
    delTechno() {
      this.$confirm('确定要删除么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const api = deleteWorkshopCraftInfo
        this.toExamineFun(api)
      })
    },
    // 订单详情
    detailPurchase(ev) {
      this.$router.push({ path: `/technologyDetail/${ev.fid}` })
    }
  }
}
</script>
<style lang="scss" scoped>
.process {
  @include listBom;
  .header {
    position: relative;

    .header-name {
      width: 100%;
    }

    .btn {
      transform: translateY(18%);
      margin-left: 450px !important;
      z-index: 999;
    }
    .btn1 {
      margin-left: 20px;
    }
  }
}
</style>
